package com.example.icampusevent;

import java.util.ArrayList;
import java.util.Calendar;

import android.os.Bundle;
import android.os.StrictMode;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

public class SearchActivity extends Activity {
	
	private String userId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        
        this.userId = getIntent().getStringExtra("USERID");

        // Disable the built-in main action bar
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        
        setDatePicker();
        
        // set OnClickListener for search button
        Button searchButton = (Button)findViewById(R.id.search_button);
        searchButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				ArrayList<String> selectedCategories = getCheckedCategory();
				if(selectedCategories == null) {
					return;
				}
				
				EditText edit = (EditText)findViewById(R.id.searchKeyword);
				String keyword = edit.getText().toString().trim();
				
				Calendar c = Calendar.getInstance();
				DatePicker datePicker = (DatePicker)findViewById(R.id.date_picker);
		        c.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
		        c.set(Calendar.HOUR_OF_DAY, 0);
		        c.set(Calendar.MINUTE, 0);
		        c.set(Calendar.SECOND, 0);
		        long startTime = c.getTimeInMillis();
		        
		        EventListFragment eventListFragment = (EventListFragment)getFragmentManager().
		    			findFragmentByTag("event_list_fragment");
		        if(eventListFragment == null) {
		    		toast("Error: cannot update event list.");
		    		return;
		    	}
		        eventListFragment.updateEventList(selectedCategories, keyword, startTime);
			}
        });
        
        EventListFragment eventListFragment = (EventListFragment)getFragmentManager().
    			findFragmentByTag("event_list_fragment");
        eventListFragment.initEventList();
    }
    
    
    @Override
    public void onResume() {
    	super.onResume();
    	Intent intent = getIntent();
        this.userId = intent.getStringExtra("USERID");
    }
    
    @Override
    public void onNewIntent(Intent newIntent) {
    	setIntent(newIntent);
    }
    
    
    private ArrayList<String> getCheckedCategory() {
    	EventCategoryFragment categoryFragment = (EventCategoryFragment)getFragmentManager().
    			findFragmentByTag("event_category_fragment");
    	if(categoryFragment == null) {
    		toast("Error: cannot get checked category from fragment.");
    		return null;
    	}
    	return categoryFragment.getCheckedCategory();
    }
    
    
    // Set the DatePicker
    private void setDatePicker() {
    	DatePicker datePicker = (DatePicker)findViewById(R.id.date_picker);
        datePicker.setCalendarViewShown(false);
    }
    
    
    public String getUserId() {
    	return this.userId;
    }
    
    public void toast(String s) {
		CharSequence text = s;
		int duration = Toast.LENGTH_LONG;
		Toast toast = Toast.makeText(this, text, duration);
		toast.show();
	}
}
